package com.bnlr.lrparking.appui.chose;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.ListView;
import android.widget.TextView;

import com.bnlr.lrparking.R;
import com.bnlr.lrparking.base.BaseActivity;
import com.bnlr.lrparking.util.city.City;
import com.bnlr.lrparking.util.city.CityListAdapter;
import com.bnlr.lrparking.util.city.DBManager;
import com.bnlr.lrparking.util.city.LocateState;
import com.bnlr.lrparking.util.city.SideLetterBar;

import java.util.List;

/**
 * 选择城市
 */
public class ChoseCityActivity extends BaseActivity {

    public static final String KEY_PICKED_CITY = "picked_city";
    private TextView tvNavTitle;
    private DBManager dbManager;
    private List<City> mAllCities;
    private ListView mListView;
    private SideLetterBar mLetterBar;
    private CityListAdapter mCityAdapter;
    private String currentCity;
    private String fromTag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chose_city);
        tvNavTitle = (TextView) findViewById(R.id.tv_nav_title);
        tvNavTitle.setText("请选择城市");
        Intent intent = getIntent();
        currentCity = intent.getStringExtra("city_name");
        fromTag = intent.getStringExtra("from");
        mListView = (ListView) findViewById(R.id.listview_all_city);
        TextView overlay = (TextView) findViewById(R.id.tv_letter_overlay);
        mLetterBar = (SideLetterBar) findViewById(R.id.side_letter_bar);
        mLetterBar.setOverlay(overlay);
        mLetterBar.setOnLetterChangedListener(new SideLetterBar.OnLetterChangedListener() {
            @Override
            public void onLetterChanged(String letter) {
                int position = mCityAdapter.getLetterPosition(letter);
                mListView.setSelection(position);
            }
        });
        initData();
    }

    private void initData() {
        dbManager = new DBManager(this);
        dbManager.copyDBFile();
        mAllCities = dbManager.getAllCities();
        mCityAdapter = new CityListAdapter(this, mAllCities);
        mListView.setAdapter(mCityAdapter);
        mCityAdapter.updateLocateState(LocateState.SUCCESS, currentCity);
        mCityAdapter.setOnCityClickListener(new CityListAdapter.OnCityClickListener() {
            @Override
            public void onCityClick(String name) {
                backWithData(name);
            }

            @Override
            public void onLocateClick() {
                mCityAdapter.updateLocateState(LocateState.LOCATING, null);
            }
        });
    }

    private void backWithData(String city) {
        if (currentCity != null) {
            if (!TextUtils.isEmpty(city) && !city.contains("定位中")) {
                Intent data = new Intent();
                data.putExtra(KEY_PICKED_CITY, city);
                setResult(2222, data);
                finish();
            }
        } else if (!TextUtils.isEmpty(fromTag) && fromTag.equals("map")) {
            Intent data = new Intent();
            data.putExtra(KEY_PICKED_CITY, city);
            setResult(1600, data);
            finish();
        }
    }
}
